let sest;
let content;

$(function() {
	sest = sessionStorage;
	content = $("#content");
	exec();
	imgError();
	let url = location.href;
	let i = url.lastIndexOf("load");
	if(i<0){
		load(sest.getItem("msg"));
	}else{
		load(url.substr(i+5));
	}
	//load(sest.getItem("msg"));
});

/**
 * 图片加载失败，显示默认图片
 */
let imgError = function(){
	document.addEventListener("error", function(e){
		let elem = e.target;
		if($(elem).is("[data-err]")){
			$(elem).attr("src", "../img/_noimg.png");
		}
	}, true);/*指定事件处理函数在捕获阶段执行*/
}

/**
 * 
 */
let exec = function(){
	$("li[data-load]").on("click", function(){
		load($(this).data("load"));
	});
}

let load = function(e){
	if(e == undefined || e==null){
		sest.setItem("msg", "greatInform()");
	}else{
		sest.setItem("msg", e);
	}
	$("[data-load = '"+sest.getItem("msg")+"']").addClass("thisclass").siblings().removeClass("thisclass");
	eval(sest.getItem("msg"));
}

/**
 * 点赞通知
 */
let greatInform = function() {
	$.ajax({
		type:"get",
		url:"great-inform.do",
		async:true,
		/*data:{}*/
		success: function(data){
			content.empty();
			if(Object.keys(data).length <= 0){
				ps.clone().appendTo(content);
			}else{
				$.each(data, function(i, j){
					let grs = greats.clone().appendTo(content);
					grs.find(".photo-title>a").text(j.titleName);
					grs.find(".photo-title>sub").text(j.releaseTime);
					let thumbnail = grs.find(".photo-thumbnail");
					$.each(j.photoAlbumMap, function(k, l) {
						imgs.clone().attr("src", l[0]).attr("data-url", l[1]).appendTo(thumbnail);
					});
					let great = grs.find(".photo-great");
					$.each(j.visitors, function(m, n) {
						userMsg(great, n);
					});
				});
			}
		},dataType:"json"
	});
}

let map;

/**
 * 收藏通知
 */
let collectInform = function() {
	$.ajax({
		type:"get",
		url:"collect-inform.do",
		async:true,
		/*data:{};*/
		success: function(data){
			content.empty();
			map = undefined;
			map = data.photoAlbumMap;
			if(data.visitors == null || data.visitors <= 0){
				ps.clone().appendTo(content);
			}else{
				$.each(data.visitors, function(i, j) {
					let divs = $("<div></div>").appendTo(content);
					userMsg(divs, j);
					imgSet(divs, map, j.photoProductId);
				});
			}
		},dataType:"json"
	});
}

/**
 * 评论通知
 */
let comment = function(){
	$.ajax({
		type:"get",
		url:"comment-inform.do",
		async:true,
		/*data:{};*/
		success: function(data){
			console.info(data);
			content.empty();
			if(data.length <= 0){
				ps.clone().appendTo(content);
			}else{
				$.each(data, function(i, v) {
					let comments = commentary.clone().appendTo(content);
					comments.find("img").attr("src", v.userImg).attr("data-photoproductid", v.photoProductId);
					comments.find(".head-title>a").text(v.userName);
					comments.find(".head-title>sub").text(v.photoTriggerTime);
					comments.find(".body-content").text(v.commentContent);
				});
			}
		},dataType:"json"
	});
}

/**
 * 用户信息
 * @param {Object} container
 * @param {Object} obj
 */
let userMsg = function(container, obj){
	let med = medias.clone().appendTo(container);
	med.find(".media-left").append(imgs.clone().addClass("img-circle").attr("src", obj.userImg));
	med.find("h2").text(obj.userName);
	med.find("p").text(obj.photoTriggerTime);
}

/**
 * 图片设置
 * @param {Object} container
 * @param {Object} array
 * @param {Object} obj
 */
let imgSet = function(container, array, obj){
	$.each(array, function(item, value) {
		if(item == obj){
			imgs.clone().addClass("picture photo-ml-10").attr("src", value[0]).attr("data-url", value[1]).appendTo(container);
			return;
		}
	});
}

let imgs = $(`<img data-err src="../img/_noimg.png"/>`);

let ps = $(`<p class="text-center photo-color-9" style="margin-top: 150px;font-size: 40px;">暂无相关内容</p>`);

let greats = $(`
	<div class="context-main" >
		<div class="photo-thumbnail">
		</div>
		<div class="photo-title">
			<a></a>
			<sub class="photo-color-9 photo-ml-10"></sub>
		</div>
		<div class="photo-great">
		</div>
	</div>
`);

let medias = $(`
	<div class="media media-custom">
		<div class="media-left" >
		</div>
		<div class="media-body message">
			<h2 class="media-heading photo-mt-7 phtot-font-20"></h2>
			<p class="photo-color-9"></p>
		</div>
	</div>
`);

let commentary = $(`
	<div class="context-main">
		<div class="logo-img"><img data-err style="height:60px;"/></div>
		<div class="context-right">
			<div class="head-title">
				<a class="phtot-font-20"></a>
				<sub class="photo-color-9"></sub>
			</div>
			<div class="body-content photo-color-9 h4"></div>
		</div>
	</div>
`);
